<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>数字史馆网络祭扫</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      body {
        background: url("images/memorial-bg.jpg") no-repeat center center fixed;
        background-size: cover;
        min-height: 100vh;
        font-family: "Microsoft YaHei", sans-serif;
        padding: 20px;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        height: 100vh;
        margin: 0;
      }

      .header {
        padding: 10px;
        display: flex;
        align-items: center;
        position: relative;
        margin-bottom: 20px;
      }

      .logo {
        width: 40px;
        height: 40px;
      }

      .title {
        color: #ffeb3b;
        margin-left: 10px;
        font-size: 20px;
        text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
      }

      .heroes-container {
        width: 90%;
        max-width: 1400px;
        margin: 0 auto;
        padding: 30px 50px;
        background: rgba(255, 255, 255, 0.5);
        display: grid;
        grid-template-columns: repeat(auto-fit, 120px);
        gap: 25px 60px;
        justify-content: start;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
        border-radius: 8px;
        overflow-y: auto;
        max-height: calc(100vh - 140px);
      }

      .hero-card {
        width: 120px;
        text-align: center;
        cursor: pointer;
        position: relative;
        padding-bottom: 0;
        display: flex;
        flex-direction: column;
      }

      .hero-card:hover {
        transform: scale(1.02);
      }

      .image-container {
        width: 100%;
        height: 150px;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        background: none;
        position: relative;
        border-radius: 4px;
        margin-bottom: 0;
      }

      .hero-image {
        width: 100%;
        height: 150px;
        object-fit: contain;
        object-position: top center;
        background: none;
        border-radius: 4px;
        display: block;
        transform: none;
      }

      .hero-name {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(205, 133, 63, 0.5);
        color: white;
        padding: 3px 0;
        font-size: 14px;
        text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
        box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3),
          inset 0 -1px 2px rgba(255, 255, 255, 0.2);
        z-index: 1;
        border-bottom-left-radius: 4px;
        border-bottom-right-radius: 4px;
      }

      .hero-name:before {
        content: "";
        position: absolute;
        top: -4px;
        left: 0;
        right: 0;
        height: 4px;
        background: linear-gradient(to bottom, rgba(0, 0, 0, 0.2), transparent);
      }

      .close-btn {
        position: fixed;
        top: 15px;
        right: 15px;
        font-size: 30px;
        color: #fff;
        cursor: pointer;
        background: none;
        border: none;
        z-index: 1000;
        text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
      }

      .music-btn {
        position: fixed;
        top: 15px;
        right: 60px;
        width: 35px;
        height: 35px;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
      }

      /* 添加媒体查询 */
      @media screen and (max-width: 1200px) {
        .heroes-container {
          padding: 20px 30px;
          gap: 20px 40px;
          justify-content: start;
        }
      }

      @media screen and (max-width: 768px) {
        .heroes-container {
          width: 95%;
          padding: 15px 20px;
          gap: 15px 30px;
          justify-content: start;
        }

        .hero-card {
          width: 100px;
          padding-bottom: 0;
        }

        .hero-image,
        .image-container {
          height: 130px;
        }

        .hero-name {
          font-size: 12px;
        }
      }

      /* 自定义滚动条样式 */
      .heroes-container::-webkit-scrollbar {
        width: 8px;
      }

      .heroes-container::-webkit-scrollbar-track {
        background: rgba(255, 255, 255, 0.1);
        border-radius: 4px;
      }

      .heroes-container::-webkit-scrollbar-thumb {
        background: rgba(205, 133, 63, 0.5);
        border-radius: 4px;
      }

      .heroes-container::-webkit-scrollbar-thumb:hover {
        background: rgba(205, 133, 63, 0.7);
      }

      /* 添加弹窗样式 */
      .hero-modal {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.7);
        z-index: 1000;
        display: none;
        justify-content: center;
        align-items: center;
      }

      .modal-content {
        background: white;
        padding: 30px;
        border-radius: 8px;
        width: 80%;
        max-width: 600px;
        position: relative;
        display: flex;
        gap: 20px;
      }

      .modal-close {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 24px;
        cursor: pointer;
        color: #666;
      }

      .modal-image {
        width: 200px;
        height: 250px;
        object-fit: cover;
        border-radius: 4px;
      }

      .modal-info {
        flex: 1;
      }

      .modal-name {
        font-size: 24px;
        margin-bottom: 15px;
        color: #333;
      }

      .modal-description {
        line-height: 1.6;
        color: #666;
      }

      /* 移动端适配 */
      @media screen and (max-width: 768px) {
        .modal-content {
          flex-direction: column;
          align-items: center;
          padding: 20px;
          margin: 20px;
        }

        .modal-image {
          width: 160px;
          height: 200px;
          margin-bottom: 15px;
        }

        .modal-info {
          padding-left: 0;
          width: 100%;
        }

        .modal-name {
          text-align: center;
        }
      }
    </style>
  </head>
  <body>
    <div class="header">
      <img src="images/logo.png" alt="数字史馆" class="logo" />
      <h1 class="title">数字史馆网络祭扫</h1>
    </div>

    <div class="heroes-container"></div>

    <audio id="bgMusic" loop>
      <source src="audio/background-music.mp3" type="audio/mpeg" />
    </audio>

    <!-- 添加弹窗结构 -->
    <div class="hero-modal">
      <div class="modal-content">
        <span class="modal-close">&times;</span>
        <img class="modal-image" src="" alt="" />
        <div class="modal-info">
          <h2 class="modal-name"></h2>
          <div class="modal-description"></div>
        </div>
      </div>
    </div>

    <script>
      // 加载英雄列表
      async function loadHeroes() {
        try {
          const response = await fetch("/admin/api/heroes/list.php");
          const result = await response.json();

          if (!result.success) throw new Error(result.message);

          const heroes = result.data;
          // 按 sort 字段排序（数字越大越靠前）
          heroes.sort((a, b) => b.sort - a.sort);

          const heroesContainer = document.querySelector(".heroes-container");
          heroesContainer.innerHTML = heroes
            .map(
              (hero) => `
            <div class="hero-card">
              <div class="image-container">
                <img src="${hero.image}" alt="${hero.name}" class="hero-image" />
              </div>
              <div class="hero-name">${hero.name}</div>
            </div>
          `
            )
            .join("");

          // 重新绑定点击事件
          bindHeroClickEvents(heroes);
        } catch (error) {
          console.error("加载失败:", error);
          alert("加载失败，请重试");
        }
      }

      // 绑定英雄卡片点击事件
      function bindHeroClickEvents(heroes) {
        const heroCards = document.querySelectorAll(".hero-card");
        heroCards.forEach((card, index) => {
          card.addEventListener("click", () => {
            const hero = heroes[index];
            modalImage.src = hero.image;
            modalName.textContent = hero.name;
            modalDescription.innerHTML = hero.description;
            modal.style.display = "flex";
          });
        });
      }

      const modal = document.querySelector(".hero-modal");
      const modalClose = document.querySelector(".modal-close");
      const modalImage = document.querySelector(".modal-image");
      const modalName = document.querySelector(".modal-name");
      const modalDescription = document.querySelector(".modal-description");

      // 点击关闭按钮关闭弹窗
      modalClose.addEventListener("click", () => {
        modal.style.display = "none";
      });

      // 点击弹窗外部关闭弹窗
      modal.addEventListener("click", (e) => {
        if (e.target === modal) {
          modal.style.display = "none";
        }
      });

      // 页面加载时获取英雄列表
      document.addEventListener("DOMContentLoaded", loadHeroes);
    </script>
  </body>
</html>
